package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.pojo.Bed;
import com.xie.pojo.Dept;
import com.xie.pojo.Vo.DeptVo;
import com.xie.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "部门管理")
@RestController
@RequestMapping("/Depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 分页
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("{pageIndex}/{pageSize}")
    @ApiOperation("获取分页部门数据")
    public Result findDept(@PathVariable Integer pageIndex
            , @PathVariable Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage<Dept> page1 = deptService.page(page);
        if (page1 != null) return Result.ok(page1);
        return Result.fail();
    }

    /**
     * 部门模糊查询
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询部门信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Dept> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Dept::getDeptId,params)
                .or().like(Dept::getDeptManager,params)
                .or().like(Dept::getDeptName,params)
                .or().like(Dept::getDeptNo,params)
                .or().like(Dept::getDeptViceManager,params)
        ;
        List<Dept> list = deptService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加
     * @param dept
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加部门数据")
    public Result addDept(@RequestBody Dept dept) {
        boolean save = deptService.save(dept);
        if (!save) return Result.fail();
        return Result.ok();
    }


    /**
     * 修改
     * @param dept
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改部门数据")
    public Result updateDept(@RequestBody Dept dept) {
        boolean u = deptService.updateById(dept);
        if (!u) return Result.fail();
        return Result.ok();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据部门id获取数据")
    public Result findDeptById(@PathVariable Integer id) {
        Dept de2= deptService.getById(id);
        if (de2 != null) return Result.ok(de2);
        return Result.fail();
    }


    /**
     * 获取全部
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取所有部门数据")
    public Result list(){
        List<Dept> depts = deptService.list();
        if (depts != null) return Result.ok(depts);
        return Result.fail();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除部门数据")
    public Result remove(@PathVariable Long id){
        boolean b = deptService.removeById(id);
        if (b)return Result.ok();
        return Result.fail();
    }


    /**
     * 联表查询
     * @return
     */
    @GetMapping("/findDeptPersonnel")
    @ApiOperation("联表查询部门和员工信息")
    public Result findClassStudent() {
        List<DeptVo> dep = deptService.findDeptPersonnel();
        if ( dep != null) return Result.ok(dep);
        return  Result.fail();
    }

}
